/**
 * 音效类
 */
class SoundEffects extends BaseSound {
    private _currSound:egret.Sound;
    private _curSoundChannel: egret.SoundChannel;
    private _isCoverEft = {};
    private _volume: number;

    /**
     * 构造函数
     */
    public constructor() {
        super();
    }

    /**
     * 停止当前音乐
     */
    public stop(): void {
        if (this._curSoundChannel) {
            this._curSoundChannel.stop();
        }
        this._curSoundChannel = null;
        this._currSound = null;
    }

    /**
     * 播放一个音效
     * @param effectName
     */
    public play(effectName: string, cover: boolean = false): void {
        this._isCoverEft[effectName] = cover;

        var sound: egret.Sound = this.getSound(effectName);
        if (sound) {
            this.playSound(sound, effectName);
        }
    }

    /**
     * 播放
     * @param sound
     */
    private playSound(sound: egret.Sound, effectName: string): void {
        let isCover = this._isCoverEft[effectName];
        if (isCover) {
            this.stop();
            this._currSound = sound;
            this._curSoundChannel = sound.play(0, 1);
            if (this._volume >= 0 && this._volume <= 1) {
                this._curSoundChannel.volume = this._volume;
            }
        } else {
            let soundChannel = sound.play(0, 1);
            if (this._volume >= 0 && this._volume <= 1) {
                soundChannel.volume = this._volume;
            }
        }
    }

    /**
     * 设置音量
     * @param volume
     */
    public setVolume(volume: number): void {
        this._volume = volume;
    }


    /**
     * 资源加载完成后处理播放
     * @param key
     */
    public loadedPlay(key: string): void {
        if (RES.getRes(key)) {
            this.playSound(RES.getRes(key), key);
        } else {
            console.log("没有加载出来音效：" + key);
        }
    }

    public checkCanClear(key: string): boolean {
        return true;
    }
}